package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.dto.DecrptWechatRunDTO;
import com.example.demo.utils.Constant;
import com.example.demo.utils.RedisUtils;
import com.example.demo.utils.WechatUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <h3>wechat-token</h3>
 * <p></p>
 *
 * @author : zhengyue
 * @date : 2021-02-20 10:39
 **/
@RestController
@RequestMapping("wechat")
@Slf4j
public class WechatController {
    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private WechatUtils wechatUtils;

    /***
     * @Description: 获取accessToken
     * @Author: zhengyue
     * @Date: 2021/5/18 15:47
     * @Param: []
     * @Return: java.lang.String
     */
    @GetMapping("accessToken")
    public String accessToken() {
        String token = redisUtils.get(Constant.ACCESS_TOKEN);
        if(StringUtils.isEmpty(token)) {
            return "access_token已过期，检查appid或者secret是否正确";
        }
        return token;
    }
    
    /**
     * @Description: 获取session_key
     * @Author: zhengyue
     * @Date: 2021/5/18 15:48
     * @Param: 
     * @Return: 
     */
    @GetMapping("sessionKey")
    public String sessionKey(String jscode) {
        return wechatUtils.getSessionKey(jscode).getSession_key();
    }

    /**
     * @Description: 获取openid
     * @Author: zhengyue
     * @Date: 2021/5/18 15:48
     * @Param:
     * @Return:
     */
    @GetMapping("openid")
    public String openid(String jscode) {
        return wechatUtils.getSessionKey(jscode).getOpenid();
    }

    /**
     * @Description: 获取微信运动数据
     * @Author: zhengyue
     * @Date: 2021/5/19 11:57
     * @Param: [dto]
     * @Return: java.lang.String
     */
    @GetMapping("/runData")
    public String runData(@Validated  DecrptWechatRunDTO dto) {
        // get请求会处理掉+号，所以这里再转换一下
        String encryptedData = dto.getEncryptedData()
                .replace(" ", "+");
        String iv = dto.getIv()
                .replace(" ", "+");
        String sessionKey = dto.getSessionKey()
                .replace(" ", "+");
        String decrypt = null;
        //解密
        try {
            // {"stepInfoList":[{"timestamp":1618675200,"step":6995},{"timestamp":1618761600,"step":4369}
            decrypt = wechatUtils.decryptWeChatRun(sessionKey, encryptedData, iv);
        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONObject stepInfo = JSON.parseObject(decrypt);
        // 获取一个月内的步数
        JSONArray stepInfoList = JSON.parseArray(stepInfo.getString("stepInfoList"));
        //获取今天的步数，一般是数组最后一个
        JSONObject today = stepInfoList.getJSONObject(stepInfoList.size() - 1);
        return today.getString("step");
    }

}
